package ioperation;

import book.Book;
import book.BookList;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: dings
 * Date: 2024-05-24
 * Time: 18:49
 */
public class SortByPrice implements IOperation{
    @Override
    public void work(BookList bookList) {
        System.out.println();
        PriceComparator priceComparator = new PriceComparator();
        int currentSize = bookList.getUsedSize();
        for (int i = 0; i < currentSize - 1; i++) {
            boolean flg = false;
            for (int j = 0; j < currentSize - 1 - i; j++) {
                Book book1 = bookList.getBook(j);
                Book book2 = bookList.getBook(j + 1);
                if(priceComparator.compare(book1,book2) > 0) {
                    bookList.setBook(j,book2);
                    bookList.setBook(j+1,book1);
                    flg = true;
                }
            }
            if(flg == false) {
                break;
            }
        }
        System.out.println("所有图书已按价格排序成功！！！");
        System.out.println();
    }
}
